<?php

//小站社区模型
class Forum_model extends Model {

	function Forum_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("comment_model");
		$this->load->model("accounts_model", "am");
		$this->load->model("site_model", "sm");
		$this->load->model("classes_model");
	}
	
	//获取列表
	function listitem($offset = 0, $pagecount = 20, $order = "id", $desc = "desc")
	{
		$items = array();
		$query = $this->db->from("module_forum")->limit($pagecount, $offset)->order_by($order, $desc)->get();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
		}
		return $items;
	}
	
	//计算总数
	function countitems()
	{
		return $this->db->count_all_results("module_forum");
	}
	
	//更改分类
	function update_class($siteid, $classid)
	{
		//获取顶级分类ID
		$classid = $this->classes_model->get_topclassid($classid);
		$data = array(
			"classid" => $classid,
		);
		$this->db->where("siteid", $siteid);
		$result = $this->db->update("module_forum", $data);
		return $result;
	}
	
	//推荐话题
	function recommendselected($delstr, $updata = 1)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//屏蔽会员
		$data = array(
			"recommend" => $updata,
		);
		$this->db->where_in("id", $delid);
		$result = $this->db->update("module_forum", $data);
		return $result;
	}
	
	//删除选择话题
	function delselected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//删除评论
//		$this->comment_model->forum_delete($id);
		$this->db->where_in("dataid", $delid);
		$this->db->delete("module_forum_comment");
		$this->db->where_in("id", $delid);
		$result = $this->db->delete("module_forum");
		return $result;
	}
	
	//删除话题
	function delete($id)
	{
		//删除评论
//		$this->comment_model->forum_delete($id);
		$this->db->where("dataid", $id);
		$this->db->delete("module_forum_comment");
		$result = $this->db->delete("module_forum", array("id"=>$id));
		return $result;
	}
	
	//查看话题
	function show($id)
	{
		$row = array();
		$query = $this->db->get_where("module_forum", array("id"=>$id));
		if($query->num_rows()>0) {
			$row = $query->row_array();
			//用户信息
			$userid = $row["userid"];
			$userinfo = $this->am->get($userid);
			$row["userinfo"] = $userinfo;
			//评论总数
			$commentcount = $this->comment_model->count_forum_lists($row["siteid"], $row["id"]);
			$row["commentcount"] = $commentcount;
			//小站信息
			$siteid = $row["siteid"];
			$row["site"] = $this->sm->get_site($siteid);
		}
		return $row;
	}
	
	//获取用户话题列表
	function lists_user($userid, $limit = 2, $offset = 0, $order = "id", $sortby = "desc")
	{
		$lists = array();
		$query = $this->db->from("module_forum")->where("userid", $userid)->order_by($order, $sortby)->limit($limit, $offset)->get();
		if($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $row) {
				//获取小站信息
				$siteid = $row["siteid"];
				$site = $this->sm->get_site($siteid);
				$lists[$k]["domain"] = $site["domain"];
				//用户信息
				$userid = $row["userid"];
				$userinfo = $this->am->get($userid);
				$lists[$k]["userinfo"] = $userinfo;
				//评论总数
				$commentcount = $this->comment_model->count_forum_lists($siteid, $row["id"]);
				$lists[$k]["commentcount"] = $commentcount;
			}
		}
		return $lists;
	}
	
	//计算总数
	function count_lists_user($userid)
	{
		$query = $this->db->from("module_forum")->where(array("userid"=>$userid))->get();
		return $query->num_rows();
	}
	
	//获取列表
	function lists($siteid, $limit = 2, $offset = 0, $sortid = 1, $sortby = "desc")
	{
		$lists = array();
		if(!in_array($sortid, array(1, 2))) {
			$sortid = 1;
		}
		if($sortid == 1) {
			$order = "id";
		} elseif ($sortid == 2) {
			$order = "commentime";
		}
		$query = $this->db->from("module_forum")->where("siteid", $siteid)->order_by($order, $sortby)->limit($limit, $offset)->get();
		if($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $row) {
				//用户信息
				$userid = $row["userid"];
				$userinfo = $this->am->get($userid);
				$lists[$k]["userinfo"] = $userinfo;
				//评论总数
				$commentcount = $this->comment_model->count_forum_lists($siteid, $row["id"]);
				$lists[$k]["commentcount"] = $commentcount;
			}
		}
		return $lists;
	}
	
	//计算总数
	function count_lists($siteid)
	{
		$query = $this->db->from("module_forum")->where(array("siteid"=>$siteid))->get();
		return $query->num_rows();
	}
	
	//获取严选推荐
	function get_recommend($limit)
	{
		$lists = array();
		$query = $this->db->from("module_forum")->where("recommend >", 0)->order_by("recommend", "desc")->limit($limit)->get();
		if ($query->num_rows()>0) {
			$result = $query->result_array();
			foreach ($result as $row) {
				$id = $row["id"];
				$lists[] = $this->show($id);
			}
		} 
		return $lists;
	}
	
	//获取严选列表
	function recommend_list($limit = 10)
	{
		$lists = $hack_ids = array();
		/*$lists1 = $this->get_recommend($limit);
		if(count($lists1)>0 && count($lists1) < $limit) {//随机+手动
			foreach ($lists1 as $list) {
				$id = $list["id"];
				$hack_ids[] = $id;
			}
			//获取随机
			$limit = $limit - count($lists1);
			$lists2 = $this->lists_rang(20, $limit, $hack_ids);
			(count($lists2)>0) ? $lists = array_merge($lists1, $lists2) : $lists = $lists1;
		} elseif (count($lists1)>=$limit) {//全手动
			$lists = $lists1;
		} elseif (count($lists1) == 0) {//全随机
			$lists = $this->lists_rang(20, $limit);
		} else {
			$lists = $lists1;
		}*/
		$lists = $this->get_recommend($limit);
		return $lists;
	}
	
	//获取列表
	//$timerang 时间段多少天以内评论最多的文章:可能不足10个
	function lists_rang($timerang = 20, $limit = 10, $hack_ids =array())
	{
		$lists = $ids = array();
		//先取出时间段评论
		$limittime = time() - $timerang*86400;
		$query = $this->db->get_where("module_forum_comment", array("addtime >="=>$limittime));
		if ($query->num_rows()>0) {
			$result = $query->result_array();
			foreach ($result as $row) {
				if(!isset($ids[$row["dataid"]])) {
					$ids[$row["dataid"]] = 1;
				} else {
					$ids[$row["dataid"]] = $ids[$row["dataid"]]+1;
				}
			}
			@arsort($ids);
			$ids = array_keys($ids);
			$ids = array_slice($ids, 0, $limit);
			//再取出文章
			if(is_array($ids) && count($ids) > 0) {
				foreach ($ids as $id) {
					if(!in_array($id, $hack_ids)) {
						$lists[] = $this->show($id);
					}
				}
			}
		}
		return $lists;
	}
	
	//编辑话题
	function edit($data, $id)
	{
		$data["updatetime"] = time();
		$this->db->where("id", $id);
		$result = $this->db->update("module_forum", $data);
		return $result;
	}
	
	//发表话题
	function add($data)
	{
		$data["addtime"] = time();
		$result = $this->db->insert("module_forum", $data);
		$id = $this->db->insert_id();
		return $id;
	}
	
	//预览
	function preivew()
	{
		$viewdata = array();
		if(p("title") && p("description")) {
			$viewdata["id"] = 0;
			$viewdata["title"] = p("title");
			$viewdata["iswall"] = p("iswall");
			$viewdata["description"] = p("description");
			if(p("id")) $viewdata["id"] = p("id");//预览编辑话题
		}
		return $viewdata;
	}
	
}

/* End of file */